﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.StoreApplies
{
    public partial class AddStoreGrades : ManageBasePage
    {
        private IStoreGradeTask _storeGradeTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("AddStoreGrades");
            _storeGradeTask = GetInstance<IStoreGradeTask>();
            if (!IsPostBack)
            {
                int id = -1;
                int.TryParse(Request["ID"], out id);
                if (id > 0)
                {
                    var model = _storeGradeTask.Get(id);
                    txtGradeName.Text = model.GradeName;
                    txtShopCount.Text = model.GoodsLimit.ToString();
                    txtSpaceLimit.Text = model.SpaceLimit.ToString();
                    txtSkinLimit.Text = model.SkinLimit.ToString();
                    cbIsCanSecKill.Checked = model.CanSecKill;
                    cbIsCanPromoted.Checked = model.CanPromoted;
                    cbIsCanIsBest.Checked = model.CanIsBest;
                    cbIsCanIsHot.Checked = model.CanIsHot;
                    cbCanUploadDefinitionImg.Checked = model.CanUploadDefinitionImg;
                    cbIsCanUploadVideo.Checked = model.CanUploadVideo;
                    txtInitiaFee.Text = model.InitiaFee.ToString();
                    txtManagementFee.Text = model.ManagementFee.ToString();
                    txtGradeDesc.Text = model.GradeDesc;

                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (var work = UnitOfWork.Begin())
            {
                int id = -1;
                int.TryParse(Request["ID"], out id);
                if (id > 0)
                {
                    // 修改
                    var modelData = _storeGradeTask.Get(id);
                    var saveData = new SaveStoreGradeCommand();
                    saveData.Id = modelData.Id;
                    saveData.GradeName = txtGradeName.Text.Trim();
                    saveData.GoodsLimit = int.Parse(txtShopCount.Text.Trim());
                    saveData.SpaceLimit = int.Parse(txtSpaceLimit.Text.Trim());
                    saveData.SkinLimit = int.Parse(txtSkinLimit.Text.Trim());
                    saveData.CanSecKill = cbIsCanSecKill.Checked;
                    saveData.CanPromoted = cbIsCanPromoted.Checked;
                    saveData.CanIsBest = cbIsCanIsBest.Checked;
                    saveData.CanIsHot = cbIsCanIsHot.Checked;
                    saveData.CanUploadDefinitionImg = cbCanUploadDefinitionImg.Checked;
                    saveData.CanUploadVideo = cbIsCanUploadVideo.Checked;
                    saveData.InitiaFee = decimal.Parse(txtInitiaFee.Text.Trim());
                    saveData.ManagementFee = decimal.Parse(txtManagementFee.Text.Trim());
                    saveData.GradeDesc = txtGradeDesc.Text.Trim();
                    _storeGradeTask.Save(saveData);
                    work.Commit();
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');location.href='AddStoreGrades.aspx?ID=" + modelData.Id + "';", true);
                }
                else
                {
                    //新增
                    var saveData = new SaveStoreGradeCommand();
                    saveData.GradeName = txtGradeName.Text.Trim();
                    saveData.GoodsLimit = int.Parse(txtShopCount.Text.Trim());
                    saveData.SpaceLimit = int.Parse(txtSpaceLimit.Text.Trim());
                    saveData.SkinLimit = int.Parse(txtSkinLimit.Text.Trim());
                    saveData.CanSecKill = cbIsCanSecKill.Checked;
                    saveData.CanPromoted = cbIsCanPromoted.Checked;
                    saveData.CanIsBest = cbIsCanIsBest.Checked;
                    saveData.CanIsHot = cbIsCanIsHot.Checked;
                    saveData.CanUploadDefinitionImg = cbCanUploadDefinitionImg.Checked;
                    saveData.CanUploadVideo = cbIsCanUploadVideo.Checked;
                    saveData.InitiaFee = decimal.Parse(txtInitiaFee.Text.Trim());
                    saveData.ManagementFee = decimal.Parse(txtManagementFee.Text.Trim());
                    saveData.GradeDesc = txtGradeDesc.Text.Trim();
                    _storeGradeTask.Save(saveData);
                    work.Commit();
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');location.href='AddStoreGrades.aspx';", true);
                }
            }

            return;
        }
    }
}